package com.wans.guli.ware.service.impl;

import cn.hutool.core.util.StrUtil;
import org.springframework.stereotype.Service;

import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wans.guli.common.utils.PageUtils;
import com.wans.guli.common.utils.Query;

import com.wans.guli.ware.dao.PurchaseDetailDao;
import com.wans.guli.ware.entity.PurchaseDetailEntity;
import com.wans.guli.ware.service.PurchaseDetailService;


@Service("purchaseDetailService")
public class PurchaseDetailServiceImpl extends ServiceImpl<PurchaseDetailDao, PurchaseDetailEntity> implements PurchaseDetailService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String key = (String) params.get("key");
        boolean notEmptyKey = StrUtil.isNotEmpty(key);
        String wareId = (String) params.get("wareId");
        boolean notEmptyWareId = StrUtil.isNotEmpty(wareId);
        String status = (String) params.get("status");
        boolean notEmptyStatus = StrUtil.isNotEmpty(status);

        IPage<PurchaseDetailEntity> page = this.page(
                new Query<PurchaseDetailEntity>().getPage(params),
                new QueryWrapper<PurchaseDetailEntity>().lambda()
                        .eq(notEmptyWareId, PurchaseDetailEntity::getWareId, wareId)
                        .eq(notEmptyStatus, PurchaseDetailEntity::getStatus, status)
                        .and(notEmptyKey, wrapper -> {
                            wrapper.like(PurchaseDetailEntity::getPurchaseId, key)
                                    .or()
                                    .like(PurchaseDetailEntity::getSkuNum, key);
                        })
        );

        return new PageUtils(page);
    }

}